﻿using System;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Input;

namespace MediaAssistant.Controls.NightVision
{
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class NightVisionWindow
    {
        public NightVisionWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        private void WindowMouseMove(object sender, MouseEventArgs e)
        {
            var position = e.GetPosition(this);
            X = Math.Max(position.X - WholeWidth/2, 0);
            Y = Math.Max(position.Y - WholeHeight/2, 0);
            if(IsActive==false)
                Activate();
        }

        public double X
        {
            get { return (double)GetValue(XProperty); }
            set { SetValue(XProperty, value); }
        }

        public static readonly DependencyProperty XProperty =
            DependencyProperty.Register("X", typeof(double), typeof(NightVisionWindow), new UIPropertyMetadata(0d));

        public double Y
        {
            get { return (double)GetValue(YProperty); }
            set { SetValue(YProperty, value); }
        }

        public static readonly DependencyProperty YProperty =
            DependencyProperty.Register("Y", typeof(double), typeof(NightVisionWindow), new UIPropertyMetadata(0d));

        public double NightOpacity
        {
            get { return (double)GetValue(NightOpacityProperty); }
            set { SetValue(NightOpacityProperty, value); }
        }

        public static readonly DependencyProperty NightOpacityProperty =
            DependencyProperty.Register("NightOpacity", typeof(double), typeof(NightVisionWindow), new UIPropertyMetadata(0.9));

        public double WholeHeight
        {
            get { return (double)GetValue(WholeHeightProperty); }
            set { SetValue(WholeHeightProperty, value); }
        }

        public static readonly DependencyProperty WholeHeightProperty =
            DependencyProperty.Register("WholeHeight", typeof(double), typeof(NightVisionWindow), new UIPropertyMetadata(80d));

        public double WholeWidth
        {
            get { return (double)GetValue(WholeWidthProperty); }
            set { SetValue(WholeWidthProperty, value); }
        }

        public static readonly DependencyProperty WholeWidthProperty =
            DependencyProperty.Register("WholeWidth", typeof(double), typeof(NightVisionWindow), new UIPropertyMetadata(200d));

        private void KeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                Close();
            }
            else
            {
                const double delta = 0.05;
                if(e.Key==Key.Left)
                {
                    NightOpacity = Math.Max(0, NightOpacity - delta);
                }
                else if(e.Key==Key.Right)
                {
                    NightOpacity = Math.Min(1, NightOpacity + delta);
                }
            }
        }
    }
}
